package com.clps.controller;


import com.clps.common.CustomException;
import com.clps.common.R;
import com.clps.dto.DepositDto;
import com.clps.entity.Deposit;
import com.clps.mapper.BankcardMapper;
import com.clps.mapper.DepositMapper;
import com.clps.service.DepositService;
import io.swagger.annotations.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author ywl
 * @since 2022-09-07
 */
@RestController
@Api(tags = "存款相关控制器")
@RequestMapping("/deopsit")
public class DepositController {

    @Autowired
    DepositService depositService;
    @Autowired
    DepositMapper depositMapper;
    @Autowired
    BankcardMapper bankcardMapper;

    /**
     * 根据银行卡号进行模糊查询，默认查询全部
     * @param cardNumber
     * @return
     */
    @ApiOperation("根据银行卡搜索存款列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "cardNumber", value = "银行卡号，根据银行卡号进行模糊查询", required = false)
    })
    @GetMapping("/search")
    public R<List<Deposit>> search(@RequestParam(defaultValue = "") String cardNumber) {
        return depositService.queryByCardNumber(cardNumber);
    }

    /**
     * 新增
     * @return
     */
    @ApiOperation("添加存款记录")
    @PostMapping
    public R<String> save(@RequestBody DepositDto depositDto) {
        Deposit deposit = new Deposit();
        BeanUtils.copyProperties(depositDto,deposit,"userInfo","createTime");
        deposit.setCreateTime(LocalDateTime.now());
        deposit.setUserId(depositDto.getUserId());
        boolean flag = depositService.save(deposit);
        return flag ? R.success("保存成功") : R.error("保存失败");
    }


    @ApiOperation("根据存款id号，去查询存款记录，用于回显")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "存款记录id",required = true)
    })
    @GetMapping("/getById/{id}")
    public R<DepositDto> getDepositWithUserInfoById (@PathVariable("id") Long id){
        DepositDto depositDto = depositMapper.getDepositWithUserInfoById(id);
        if(depositDto == null){
            throw  new CustomException("请求失败");
        }
        return R.success(depositDto);
    }

    /**
     * 修改
     * @param deposit
     * @return
     */
    @ApiOperation("修改存款记录")
    @PutMapping
    public R<String> update(@RequestBody Deposit deposit) {
        boolean save = depositService.updateById(deposit);
        return save?R.success("修改成功"):R.error("修改失败");
    }


    /**
     * 删除
     * @param ids
     * @return
     */
    @ApiOperation("删除存款记录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids",value = "要删除存款记录id集合")
    })
    @DeleteMapping
    public R<String> delete(@RequestParam("ids")List<Long> ids){
        if( ids == null ||ids.size() == 0){
            throw  new CustomException("请求失败");
        }
        boolean flag = depositService.removeByIds(ids);
        return flag ? R.success("删除成功"):R.error("删除失败");
    }
}

